package com.teacold.customdemo.chapter_twelve;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class MyFlowLayout extends ViewGroup {

    public MyFlowLayout(Context context) {
        super(context);
    }

    public MyFlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyFlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int measureWidth=MeasureSpec.getSize(widthMeasureSpec);
        int measureHeight=MeasureSpec.getSize(heightMeasureSpec);
        int measureWidthMode=MeasureSpec.getMode(widthMeasureSpec);
        int measureHeightMode=MeasureSpec.getMode(heightMeasureSpec);
        int lineWitdth = 0;
        int lineHeight = 0;
        int height = 0;
        int width = 0;
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            MarginLayoutParams marginLayoutParams = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth() + marginLayoutParams.leftMargin + marginLayoutParams.rightMargin;
            int childHeigth = child.getMeasuredHeight() + marginLayoutParams.topMargin + marginLayoutParams.bottomMargin;
            if (lineWitdth + childWidth > measureWidth) {
                width = Math.max(lineWitdth, width);
                height += lineHeight;
                lineHeight=childHeigth;
                lineWitdth=childWidth;
            }else {
                lineHeight=Math.max(lineHeight,childHeigth);
                lineWitdth+=childWidth;
            }
            if(i==count-1){
                height+=lineHeight;
                width=Math.max(width,lineHeight);
            }
        }


        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count=getChildCount();
        int lineWidth=0;//累加当前的行宽
        int lineHeight=0;//当前行的行高
        int top=0,left=0;//当前控件的top坐标和left坐标
        for (int i = 0; i <count; i++) {
                View child=getChildAt(i);
                MarginLayoutParams marginLayoutParams= (MarginLayoutParams) child.getLayoutParams();
                int childWidth=child.getMeasuredWidth()+marginLayoutParams.leftMargin+marginLayoutParams.rightMargin;
                int childHeight=child.getMeasuredHeight()+marginLayoutParams.topMargin+marginLayoutParams.bottomMargin;
                if(childWidth+lineWidth>getMeasuredWidth()){
                    top+=lineHeight;
                    left=0;
                    lineHeight=childHeight;
                    lineWidth=childWidth;
                }else {
                    lineHeight=Math.max(lineHeight,childHeight);
                    lineWidth+=childWidth;
                }
                //计算childView的left,top,right,bottom
            int lc=left+marginLayoutParams.leftMargin;
            int tc=top+marginLayoutParams.topMargin;
            int rc=lc+child.getMeasuredWidth();
            int bc=tc+child.getMeasuredHeight();
            child.layout(lc,tc,rc,bc);
            left+=childWidth;

        }




    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new MarginLayoutParams(p);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }


}
